<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=0, viewport-fit=cover" />
	<title>bind</title>
</head>
<body>
<script>
var fn = function () {
	console.log(this);
	return [...arguments]
}

Function.prototype._bind = function (ctx, ...args) {
	if (typeof ctx !== 'object') throw new Error('您传进一的实参 ctx 不是一个对象')
	ctx = ctx || window;
	if (typeof this !== 'function') throw new Error('调用者不一个方法')

	const _this = this;

	return function Fn() {
		// if (new.target !== undefined) {
		if (this instanceof Fn) {
			return new _this(...args.concat([...arguments]))
		}
		else {
			ctx.fn = _this;
			const result = ctx.fn(...args.concat([...arguments]));
			delete ctx.fn;
			return result;
		}

	};
};


var func = fn._bind({ name: 'wgm' }, 1, 2)

console.log(
	new.func(3, 4)
)
</script>
</body>
</html>
